import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;


public class SalvaPreferencias {
	
	private RecordStore rs = null;
	private boolean jaExiste = false;
	
	public void openRecStore()
	{
		try{
			rs = RecordStore.openRecordStore("pref", true);
			if(rs.getNumRecords() != 0)
			{
				jaExiste = true;
			}
		}
		catch(Exception e)
		{
			e.toString();
		}
	}
	
	public void closeRecStore()
	{
		try{
			rs.closeRecordStore();
		}
		catch(Exception e)
		{
			e.toString();
		}
	}

	public void writeRecord(int registroId, String dados)
	{
		//System.out.println("dDOS: "+dados);
		byte[] data = dados.getBytes();
		try{
			//System.out.println("Next: "+ rs.getNextRecordID());
			if(rs.getNextRecordID() == registroId)
			{
				rs.addRecord(data, 0, data.length);
			}
			else
			{
				//System.out.println("Adicionei");
				rs.setRecord(registroId, data, 0, data.length);
			}
		}
		catch (NullPointerException e) {
			e.toString();
		}
		catch (Exception e) {
			e.toString();
		}
	}
	
	public byte[] readRecord(int recordId)
	{
		byte[] recData = null;
		try
		{
			recData = new byte[rs.getRecordSize(recordId)];
			recData = rs.getRecord(recordId);
		}
		catch(InvalidRecordIDException e)
		{
			//System.out.println("Erro do id");
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recData;
	}
}
